﻿using Microsoft.AspNetCore.Mvc;

using AspNetCoreWebApi.Module;

namespace AspNetCoreWebApi.Controllers
{
    [Route("api/Product")]
    [ApiController]
    public class ProductController
    {
        // 2、创建商品对象集合
        public static List<Product> Products = new List<Product>();

        [HttpGet]
        public List<Product> GetProducts()
        {
            return Products;
        }

        // 3、添加商品
        [HttpPost]
        public List<Product> PostProduct()
        {
            Products.Add(new Product()
            {
                Id = 1,
                Name = "手机"
            });
            return Products;
        }

        [HttpPut]
        public List<Product> UpdateProduct()
        {
            Products.RemoveAt(0);
            Products.Add(new Product()
            {
                Id = 1,
                Name = "电脑"
            });
            return Products;
        }

        [HttpDelete]
        public List<Product> DeleteProduct()
        {
            Products.RemoveAt(0);
            return Products;
        }

        [HttpGet]
        [Route("{ProductId}/ProductImage")]
        public List<ProductImage> GetProductImages(int ProductId)
        {
            // 1、查询商品
            Product product = Products.Where(c => c.Id == ProductId).First();
            return product.productImages;
        }

        // 3、添加商品
        [HttpPost]
        [Route("{ProductId}/ProductImage")]
        public List<Product> PostProductImages(int ProductId)
        {
            // 1、查询商品
            Product product = Products.Where(c => c.Id == ProductId).First();

            // 2、添加商品图片
            product.productImages.Add(new ProductImage()
            {
                Id = 1,
                URL = "http://fa21321321"
            });
            return Products;
        }

        [HttpPut]
        [Route("{ProductId}/ProductImage")]
        public List<Product> UpdateProductImages([FromBody] Product product)
        {
            Products.RemoveAt(0);
            Products.Add(new Product()
            {
                Id = 1,
                Name = "电脑"
            });
            return Products;
        }

        [HttpDelete]
        [Route("{ProductId}/ProductImage")]
        public List<Product> DeleteProductImages()
        {
            Products.RemoveAt(0);
            return Products;
        }

        [HttpGet]
        [Route("{ProductId}/ProductImage/{ProductImageId}/ProductImageURL")]
        public List<ProductImage> GetProductImageURLs(int ProductId,int ProductImageId)
        {
            // 1、查询商品
            Product product = Products.Where(c => c.Id == ProductId).First();
            return product.productImages;
        }

        // 3、添加商品
        [HttpPost]
        [Route("{ProductId}/ProductImage/{ProductImageId}/ProductImageURL")]
        public List<Product> PostProductImageURLs(int ProductId, int ProductImageId)
        {
            // 1、查询商品
            Product product = Products.Where(c => c.Id == ProductId).First();

            // 2、添加商品图片
            product.productImages.Add(new ProductImage()
            {
                Id = 1,
                URL = "http://fa21321321"
            });
            return Products;
        }

        [HttpPut]
        [Route("{ProductId}/ProductImage/{ProductImageId}/ProductImageURL")]
        public List<Product> UpdateProductImageURLs()
        {
            Products.RemoveAt(0);
            Products.Add(new Product()
            {
                Id = 1,
                Name = "电脑"
            });
            return Products;
        }

        [HttpDelete]
        [Route("{ProductId}/ProductImage/{ProductImageId}/ProductImageURL")]
        public List<Product> DeleteProductImageURLs()
        {
            Products.RemoveAt(0);
            return Products;
        }
    }
}
